package com.tianyang.yukuaifu.module.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;

import java.util.List;

/**
 * 文字上线轮播组件
 * Created by 薛勇军 on 2023/7/4.
 *
 * 实现思路：
 *  1、单个动画顺序：显示动画-> 开始动画-> 结束动画 循环
 *  2、前一个组件执行完显示动画后；在开始动画监听中，开始时调用下一个组件的显示动画，开始动画执行完完调用自身结束动画，并调用下一个组件的开始动画
 *  一次成为一个闭环循环。
 *
 */
public class RollTextView extends RelativeLayout {

    Context mContext;
    AnimationSet showAnim, startAnimOne, startAnimTwo, startAnimThree, endAnim;
    LinearLayout mRollLinearLayoutOne, mRollLinearLayoutTwo, mRollLinearLayoutThree;
    TextView mRollTitleOne, mRollTitleTwo, mRollTitleThree;
    TextView mRollTimeOne, mRollTimeTwo, mRollTimeThree;
    List<String> mData = null;
    int index = 0;

    public RollTextView(Context context) {
        super(context);
        this.mContext = context;
        initView();
        initData();
    }

    public RollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
        initData();
    }

    public RollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.widget_roll_text_view, this);
        mRollLinearLayoutOne = view.findViewById(R.id.mRollLinearLayoutOne);
        mRollLinearLayoutTwo = view.findViewById(R.id.mRollLinearLayoutTwo);
        mRollLinearLayoutThree = view.findViewById(R.id.mRollLinearLayoutThree);

        mRollTitleOne = view.findViewById(R.id.mRollTitleOne);
        mRollTitleTwo = view.findViewById(R.id.mRollTitleTwo);
        mRollTitleThree = view.findViewById(R.id.mRollTitleThree);

        mRollTimeOne = view.findViewById(R.id.mRollTimeOne);
        mRollTimeTwo = view.findViewById(R.id.mRollTimeTwo);
        mRollTimeThree = view.findViewById(R.id.mRollTimeThree);
    }

    public void setData(List<String> data){
        this.mData = data;
    }

    /**
     * 初始化数据
     */
    private void initData() {

        AlphaAnimation aaShow = new AlphaAnimation(0, 0.4f);
        aaShow.setDuration(2000);

        TranslateAnimation taShow = new TranslateAnimation(15, 15, 0, 0);
        taShow.setDuration(2000);

        ScaleAnimation saShow = new ScaleAnimation(0.9f, 0.9f, 0.9f, 0.9f);
        saShow.setDuration(2000);

        AlphaAnimation aaStart = new AlphaAnimation(0.4f, 1);
        aaStart.setDuration(3000);

        AlphaAnimation aaEnd = new AlphaAnimation(1, 0.4f);
        aaEnd.setDuration(3000);

        TranslateAnimation taStart = new TranslateAnimation(15, 0, 0, 60);
        taStart.setDuration(3000);

        TranslateAnimation taEnd = new TranslateAnimation(0, 15, 60, 120);
        taEnd.setDuration(3000);

        ScaleAnimation saStart = new ScaleAnimation(0.9f, 1, 0.9f, 1);
        saStart.setDuration(3000);

        ScaleAnimation saEnd = new ScaleAnimation(1, 0.9f, 1, 0.9f);
        saEnd.setDuration(3000);

//      显示动画
        showAnim = new AnimationSet(true);
        showAnim.setDuration(3000);
        showAnim.setStartOffset(1000);
        showAnim.setFillAfter(true);
        showAnim.addAnimation(aaShow);
        showAnim.addAnimation(taShow);
        showAnim.addAnimation(saShow);

//      结束动画
        endAnim = new AnimationSet(true);
        endAnim.setDuration(3000);
        endAnim.addAnimation(aaEnd);
        endAnim.addAnimation(taEnd);
        endAnim.addAnimation(saEnd);

//      第一个组件开始动画
        startAnimOne = new AnimationSet(true);
        startAnimOne.setDuration(3000);
        startAnimOne.addAnimation(aaStart);
        startAnimOne.addAnimation(taStart);
        startAnimOne.addAnimation(saStart);
        startAnimOne.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                setContentView(mRollTitleTwo, mRollTimeTwo);
                mRollLinearLayoutTwo.startAnimation(showAnim);
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                mRollLinearLayoutOne.startAnimation(endAnim);
                mRollLinearLayoutTwo.startAnimation(startAnimTwo);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

//        第二个组件开始动画
        startAnimTwo = new AnimationSet(true);
        startAnimTwo.setDuration(3000);
        startAnimTwo.addAnimation(aaStart);
        startAnimTwo.addAnimation(taStart);
        startAnimTwo.addAnimation(saStart);
        startAnimTwo.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                setContentView(mRollTitleThree, mRollTimeThree);
                mRollLinearLayoutThree.startAnimation(showAnim);
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                mRollLinearLayoutTwo.startAnimation(endAnim);
                mRollLinearLayoutThree.startAnimation(startAnimThree);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

//        第三个组件开始动画
        startAnimThree = new AnimationSet(true);
        startAnimThree.setDuration(3000);
        startAnimThree.addAnimation(aaStart);
        startAnimThree.addAnimation(taStart);
        startAnimThree.addAnimation(saStart);
        startAnimThree.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                setContentView(mRollTitleOne, mRollTimeOne);
                mRollLinearLayoutOne.startAnimation(showAnim);
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                mRollLinearLayoutThree.startAnimation(endAnim);
                mRollLinearLayoutOne.startAnimation(startAnimOne);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void setContentView(TextView titleView, TextView timeView) {
        String output = getData();
        if (output != null) {
            if (!TextUtils.isEmpty(output))
                titleView.setText(output);
//            if (!TextUtils.isEmpty(output.getCreateTime()))
//                timeView.setText(output.getCreateTime());
        }
    }

    /**
     * 启动
     */
    public void start(){
        setContentView(mRollTitleOne, mRollTimeOne);
        mRollLinearLayoutOne.startAnimation(startAnimOne);
    }

    /**
     * 获取显示的数据
     * @return s
     */
    private String getData(){
        String output = null;
        if (mData != null) {
            if (index < mData.size()) {
                output = mData.get(index);
            } else {
                int position = index%mData.size();
                output = mData.get(position);
            }
            index++;
        }
        return  output;
    }
}
